﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Framework;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class AdminProductList : UserControlBase
    {
        protected int _CatID;
        [AccessedThroughProperty("cmdAdd")]
        private LinkButton _cmdAdd;
        [AccessedThroughProperty("cmdReturn")]
        private LinkButton _cmdReturn;
        [AccessedThroughProperty("cmdSearch")]
        private LinkButton _cmdSearch;
        private string _ctl;
        [AccessedThroughProperty("ctlPagingControl")]
        private AdminPagingControl _ctlPagingControl;
        private int _CurrentPage;
        [AccessedThroughProperty("ddlCategory")]
        private DropDownList _ddlCategory;
        [AccessedThroughProperty("dgProducts")]
        private DataGrid _dgProducts;
        [AccessedThroughProperty("dlProducts")]
        private DataList _dlProducts;
        [AccessedThroughProperty("lblLineBreak")]
        private Label _lblLineBreak;
        private string _mid;
        private BaseAdmin _Parent;
        [AccessedThroughProperty("pnlGrid")]
        private Panel _pnlGrid;
        [AccessedThroughProperty("pnlList")]
        private Panel _pnlList;
        [AccessedThroughProperty("pnlSearchFocus")]
        private Panel _pnlSearchFocus;
        [AccessedThroughProperty("phPadding")]
        private PlaceHolder _phPadding;
        protected bool _Related;
        protected List<int> _RelatedList;
        private string _SkinSrc;
        [AccessedThroughProperty("txtSearch")]
        private TextBox _txtSearch;
        protected int ProdID;
        private string ResourceFile;

        public event AddButtonEventHandler AddButton;

        public event AddRelatedEventHandler AddRelated;

        public event EditCommandEventHandler EditCommand;

        public event ItemCommandEventHandler ItemCommand;

        public event ReturnButtonEventHandler ReturnButton;

        public AdminProductList()
        {
            base.Init += new EventHandler(this.Page_Init);
            this.ProdID = -1;
            this._CurrentPage = 1;
            this._ctl = "";
            this._mid = "";
            this._SkinSrc = "";
            this._CatID = -1;
            this._Related = false;
            this._RelatedList = new List<int>();
            this._Parent = null;
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            
            if (this.AddButton != null)
            {
                AddButton(RuntimeHelpers.GetObjectValue(sender), e);
            }
        }

        private void cmdReturn_Click(object sender, EventArgs e)
        {
            
            if (this.ReturnButton != null)
            {
                ReturnButton(RuntimeHelpers.GetObjectValue(sender), e);
            }
        }

        private void cmdSearch_Click(object sender, EventArgs e)
        {
            this.dgProducts.CurrentPageIndex = 0;
            this._CurrentPage = 1;
            SharedFunctions.setStoreCookieValue(this.PortalSettings.PortalId, this.ID, "SearchText", this.txtSearch.Text, 1.0);
            SharedFunctions.setStoreCookieValue(this.PortalSettings.PortalId, this.ID, "SelectedCat", this.ddlCategory.SelectedValue, 0.0);
            this._CatID = Conversions.ToInteger(this.ddlCategory.SelectedValue);
            this.populateList();
        }

        private void dgProducts_EditCommand(object source, DataGridCommandEventArgs e)
        {
            
            if (this.EditCommand != null)
            {
                EditCommand(RuntimeHelpers.GetObjectValue(source), e);
            }
        }

        private void dgProducts_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "Copy")
            {
                DataGridItem item = e.Item;
                int productID = int.Parse(e.CommandArgument.ToString());
                new ProductController().CopyProduct(productID);
                
                if (this.ItemCommand != null)
                {
                    this.ItemCommand(RuntimeHelpers.GetObjectValue(source), e);
                }
            }
        }

        private void dgProducts_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                Control control = e.Item.Controls[8].Controls[0];
                if (control is ImageButton)
                {
                    ImageButton button = (ImageButton) control;
                    button.Attributes.Add("onClick", "javascript:return confirm('" + DotNetNuke.Services.Localization.Localization.GetString("cmdCopyProduct", this.ResourceFile) + "');");
                }
                if (e.Item.Cells[4].Text == "True")
                {
                    e.Item.Cells[4].Text = "<img src=\"" + this._Parent.StoreInstallPath + "img/accept.png\" border=\"0\" />";
                }
                else
                {
                    e.Item.Cells[4].Text = "";
                }
                if (e.Item.Cells[5].Text == "True")
                {
                    e.Item.Cells[5].Text = "<img src=\"" + this._Parent.StoreInstallPath + "img/zip.png\" border=\"0\" />";
                }
                else
                {
                    e.Item.Cells[5].Text = "";
                }
                if (e.Item.Cells[6].Text == "True")
                {
                    e.Item.Cells[6].Text = "<img src=\"" + this._Parent.StoreInstallPath + "img/delete.png\" border=\"0\" />";
                }
                else
                {
                    e.Item.Cells[6].Text = "";
                }
                if (e.Item.Cells[7].Text == "True")
                {
                    e.Item.Cells[7].Text = "<img src=\"" + this._Parent.StoreInstallPath + "img/hidden.png\" border=\"0\" />";
                }
                else
                {
                    e.Item.Cells[7].Text = "";
                }
            }
        }

        private void dlProducts_ItemCommand(object source, DataListCommandEventArgs e)
        {
            object left = NewLateBinding.LateGet(e.CommandSource, null, "CommandName", new object[0], null, null, null);
            if (Operators.ConditionalCompareObjectEqual(left, "AddToWishList", false))
            {
                WishList.AddProduct(this._Parent.PortalId, e.CommandArgument.ToString(), null);
            }
            else if (Operators.ConditionalCompareObjectEqual(left, "RemoveFromWishList", false))
            {
                WishList.RemoveProduct(this._Parent.PortalId, e.CommandArgument.ToString());
            }
            else if (Operators.ConditionalCompareObjectEqual(left, "AddRelated", false))
            {
                string expression = SharedFunctions.getAdminCookieValue(this._Parent.PortalId, "ProdID");
                if (Versioned.IsNumeric(expression))
                {
                    
                    if (this.AddRelated != null)
                    {
                        AddRelated(Conversions.ToInteger(expression), Conversions.ToInteger(e.CommandArgument.ToString()));
                    }
                }
            }
            this.populateList();
        }

        public void HideAddButton()
        {
            this.cmdAdd.Visible = false;
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this._Parent = (BaseAdmin) this.Parent.Parent;
            this.ProdID = -1;
            if ((this.Request.QueryString["ProdID"] != null) && Versioned.IsNumeric(this.Request.QueryString["ProdID"]))
            {
                this.ProdID = Conversions.ToInteger(this.Request.QueryString["ProdID"]);
            }
            this._CurrentPage = 1;
            if ((this.Request.QueryString["currentpage"] != null) && Versioned.IsNumeric(this.Request.QueryString["currentpage"]))
            {
                this._CurrentPage = Conversions.ToInteger(this.Request.QueryString["currentpage"]);
            }
            this._CatID = -1;
            if ((this.Request.QueryString["CatID"] != null) && Versioned.IsNumeric(this.Request.QueryString["CatID"]))
            {
                this._CatID = Conversions.ToInteger(this.Request.QueryString["CatID"]);
            }
            this._ctl = "";
            if (this.Request.QueryString["ctl"] != null)
            {
                this._ctl = this.Request.QueryString["ctl"];
            }
            this._mid = "";
            if (this.Request.QueryString["mid"] != null)
            {
                this._mid = this.Request.QueryString["mid"];
            }
            this._SkinSrc = "";
            if (this.Request.QueryString["SkinSrc"] != null)
            {
                this._SkinSrc = this.Request.QueryString["SkinSrc"];
            }
            this._Related = false;
            if ((this.Request.QueryString["rel"] != null) && (this.Request.QueryString["rel"] == "1"))
            {
                IEnumerator enumerator=null;
                this._Related = true;
                ArrayList list = new ProductController().GetProductRelatedArray(PortalController.GetCurrentPortalSettings().PortalId, this.ProdID, SharedFunctions.GetCurrentCulture(), -1, false);
                try
                {
                    enumerator = list.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        NB_Store_ProductRelatedInfo current = (NB_Store_ProductRelatedInfo) enumerator.Current;
                        this._RelatedList.Add(current.RelatedProductID);
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
                string resourceFile = DotNetNuke.Services.Localization.Localization.GetResourceFile(this, this.GetType().BaseType.Name + ".ascx");
                string str4 = string.Format(DotNetNuke.Services.Localization.Localization.GetString("AjaxLoader", resourceFile), this.ResolveUrl("img/ajaxLoader.gif"));
                if (!this.Page.ClientScript.IsClientScriptBlockRegistered("ajaxLoader"))
                {
                    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ajaxLoader", string.Format("<script type='text/javascript'>var ajaxLoader = '{0}';</script>", str4));
                }
            }
            if (this._SkinSrc.EndsWith("Edit") | this._Related)
            {
                this.phPadding.Controls.Add(new LiteralControl("<br/><br/><br/>"));
            }
            string s = SharedFunctions.GetStoreSettingText(this._Parent.PortalId, "adminproductitem.template", SharedFunctions.GetCurrentCulture());
            string str = SharedFunctions.GetStoreSettingText(this._Parent.PortalId, "adminproductalt.template", SharedFunctions.GetCurrentCulture());
            string thumbSize = SharedFunctions.GetStoreSetting(this._Parent.PortalId, "adminproduct.thumbsize", SharedFunctions.GetCurrentCulture());
            if ((s != "") & (str != ""))
            {
                this.dlProducts.ItemTemplate = new ProductTemplate(this._Parent.TabId, this._Parent.ModuleId, Globals.ResolveUrl(this._Parent.AppRelativeTemplateSourceDirectory), thumbSize, this.Server.HtmlDecode(s), true, "", "", this._CurrentPage, this._CatID, Conversions.ToString(100), "", this._Parent.TabId, this._Parent.UserId, this._Parent.UserInfo, "", "", false, 0xf423f, this._RelatedList);
                this.dlProducts.AlternatingItemTemplate = new ProductTemplate(this._Parent.TabId, this._Parent.ModuleId, Globals.ResolveUrl(this._Parent.AppRelativeTemplateSourceDirectory), thumbSize, this.Server.HtmlDecode(str), true, "", "", this._CurrentPage, this._CatID, Conversions.ToString(100), "", this._Parent.TabId, this._Parent.UserId, this._Parent.UserInfo, "", "", false, 0xf423f, this._RelatedList);
            }
        }

        public void populateList()
        {
            int num=0;
            ProductController controller = new ProductController();
            int pagesize = 0x19;
            int num2 = 0;
            if (Versioned.IsNumeric(SharedFunctions.GetStoreSetting(this._Parent.PortalId, "adminproduct.pagesize", SharedFunctions.GetCurrentCulture())))
            {
                pagesize = Conversions.ToInteger(SharedFunctions.GetStoreSetting(this._Parent.PortalId, "adminproduct.pagesize", SharedFunctions.GetCurrentCulture()));
            }
            this.ResourceFile = DotNetNuke.Services.Localization.Localization.GetResourceFile(this, this.GetType().BaseType.Name + ".ascx");
            DataGrid dgProducts = this.dgProducts;
            DotNetNuke.Services.Localization.Localization.LocalizeDataGrid(ref dgProducts, this.ResourceFile);
            this.dgProducts = dgProducts;
            if (this._CatID == -1)
            {
                string expression = SharedFunctions.getStoreCookieValue(this.PortalSettings.PortalId, this.ID, "SelectedCat");
                if (Versioned.IsNumeric(expression))
                {
                    this._CatID = Conversions.ToInteger(expression);
                }
            }
            else if (!this.Page.IsPostBack)
            {
                SharedFunctions.setStoreCookieValue(this.PortalSettings.PortalId, this.ID, "SearchText", "", 0.0);
                SharedFunctions.setStoreCookieValue(this.PortalSettings.PortalId, this.ID, "SelectedCat", Conversions.ToString(this._CatID), 0.0);
            }
            SharedFunctions.populateCategoryList(this.PortalSettings.PortalId, this.ddlCategory, Conversions.ToString(-1), "All Categories", Conversions.ToString(this._CatID));
            this.ResourceFile = DotNetNuke.Services.Localization.Localization.GetResourceFile(this, this.GetType().BaseType.Name + ".ascx");
            dgProducts = this.dgProducts;
            DotNetNuke.Services.Localization.Localization.LocalizeDataGrid(ref dgProducts, this.ResourceFile);
            this.dgProducts = dgProducts;
            if (!this.Page.IsPostBack)
            {
                this.txtSearch.Text = SharedFunctions.getStoreCookieValue(this.PortalSettings.PortalId, this.ID, "SearchText");
            }
            if (Versioned.IsNumeric(this.ddlCategory.SelectedValue))
            {
                num = Conversions.ToInteger(this.ddlCategory.SelectedValue);
            }
            else
            {
                num = -1;
            }
            bool getArchived = true;
            if (this._Related)
            {
                getArchived = false;
            }
            ArrayList list = controller.GetProductList(this.PortalSettings.PortalId, this._CatID, SharedFunctions.GetCurrentCulture(), this.txtSearch.Text, getArchived, this._CurrentPage, pagesize, SharedFunctions.GetStoreSettingBoolean(this.PortalSettings.PortalId, "searchdescription.flag", SharedFunctions.GetCurrentCulture()), true);
            num2 = controller.GetProductListSize(this.PortalSettings.PortalId, this._CatID, SharedFunctions.GetCurrentCulture(), this.txtSearch.Text, getArchived, false, SharedFunctions.GetStoreSettingBoolean(this.PortalSettings.PortalId, "searchdescription.flag", SharedFunctions.GetCurrentCulture()), true, "", false);
            if (this._Related)
            {
                if (Versioned.IsNumeric(SharedFunctions.GetStoreSetting(this._Parent.PortalId, "adminproduct.repeatcolumns", SharedFunctions.GetCurrentCulture())))
                {
                    this.dlProducts.RepeatColumns = Conversions.ToInteger(SharedFunctions.GetStoreSetting(this._Parent.PortalId, "adminproduct.repeatcolumns", SharedFunctions.GetCurrentCulture()));
                }
                else
                {
                    this.dlProducts.RepeatColumns = 4;
                }
                this.cmdAdd.Visible = false;
                string str2 = DotNetNuke.Services.Localization.Localization.GetString("cmdReturn", this.ResourceFile);
                this.cmdReturn.Text = str2;
                this.dlProducts.DataSource = list;
                this.dlProducts.DataBind();
                this.dgProducts.Visible = false;
                this.ctlPagingControl.QuerystringParams = "ctl=" + this._ctl + "&mid=" + this._mid + "&CatID=" + Conversions.ToString(num) + "&ProdID=" + Conversions.ToString(this.ProdID) + "&rel=1";
            }
            else
            {
                this.cmdReturn.Visible = false;
                this.dgProducts.DataSource = list;
                this.dgProducts.DataBind();
                this.dlProducts.Visible = false;
                this.ctlPagingControl.QuerystringParams = "ctl=" + this._ctl + "&mid=" + this._mid + "&CatID=" + Conversions.ToString(num);
            }
            this.ctlPagingControl.TotalRecords = num2;
            this.ctlPagingControl.PageSize = pagesize;
            this.ctlPagingControl.CurrentPage = this._CurrentPage;
            this.ctlPagingControl.TabID = this.PortalSettings.ActiveTab.TabID;
            this.ctlPagingControl.BorderWidth = 0;
            this.ctlPagingControl.SkinSrc = this._SkinSrc;
            if ((num2 <= pagesize) | (pagesize == -1))
            {
                this.ctlPagingControl.Visible = false;
                this.lblLineBreak.Visible = false;
            }
            else
            {
                this.ctlPagingControl.Visible = true;
                this.lblLineBreak.Visible = true;
            }
        }

        protected virtual LinkButton cmdAdd
        {
            get
            {
                return this._cmdAdd;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdAdd_Click);
                if (this._cmdAdd != null)
                {
                    this._cmdAdd.Click -= handler;
                }
                this._cmdAdd = value;
                if (this._cmdAdd != null)
                {
                    this._cmdAdd.Click += handler;
                }
            }
        }

        protected virtual LinkButton cmdReturn
        {
            get
            {
                return this._cmdReturn;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdReturn_Click);
                if (this._cmdReturn != null)
                {
                    this._cmdReturn.Click -= handler;
                }
                this._cmdReturn = value;
                if (this._cmdReturn != null)
                {
                    this._cmdReturn.Click += handler;
                }
            }
        }

        protected virtual LinkButton cmdSearch
        {
            get
            {
                return this._cmdSearch;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdSearch_Click);
                if (this._cmdSearch != null)
                {
                    this._cmdSearch.Click -= handler;
                }
                this._cmdSearch = value;
                if (this._cmdSearch != null)
                {
                    this._cmdSearch.Click += handler;
                }
            }
        }

        protected virtual AdminPagingControl ctlPagingControl
        {
            get
            {
                return this._ctlPagingControl;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._ctlPagingControl = value;
            }
        }

        protected virtual DropDownList ddlCategory
        {
            get
            {
                return this._ddlCategory;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._ddlCategory = value;
            }
        }

        protected virtual DataGrid dgProducts
        {
            get
            {
                return this._dgProducts;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                DataGridItemEventHandler handler = new DataGridItemEventHandler(this.dgProducts_ItemDataBound);
                DataGridCommandEventHandler handler2 = new DataGridCommandEventHandler(this.dgProducts_ItemCommand);
                DataGridCommandEventHandler handler3 = new DataGridCommandEventHandler(this.dgProducts_EditCommand);
                if (this._dgProducts != null)
                {
                    this._dgProducts.ItemDataBound -= handler;
                    this._dgProducts.ItemCommand -= handler2;
                    this._dgProducts.EditCommand -= handler3;
                }
                this._dgProducts = value;
                if (this._dgProducts != null)
                {
                    this._dgProducts.ItemDataBound += handler;
                    this._dgProducts.ItemCommand += handler2;
                    this._dgProducts.EditCommand += handler3;
                }
            }
        }

        protected virtual DataList dlProducts
        {
            get
            {
                return this._dlProducts;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                DataListCommandEventHandler handler = new DataListCommandEventHandler(this.dlProducts_ItemCommand);
                if (this._dlProducts != null)
                {
                    this._dlProducts.ItemCommand -= handler;
                }
                this._dlProducts = value;
                if (this._dlProducts != null)
                {
                    this._dlProducts.ItemCommand += handler;
                }
            }
        }

        protected virtual Label lblLineBreak
        {
            get
            {
                return this._lblLineBreak;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._lblLineBreak = value;
            }
        }

        protected virtual Panel pnlGrid
        {
            get
            {
                return this._pnlGrid;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._pnlGrid = value;
            }
        }

        protected virtual Panel pnlList
        {
            get
            {
                return this._pnlList;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._pnlList = value;
            }
        }

        protected virtual Panel pnlSearchFocus
        {
            get
            {
                return this._pnlSearchFocus;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._pnlSearchFocus = value;
            }
        }

        protected virtual PlaceHolder phPadding
        {
            get
            {
                return this._phPadding;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._phPadding = value;
            }
        }

        protected virtual TextBox txtSearch
        {
            get
            {
                return this._txtSearch;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._txtSearch = value;
            }
        }

        public delegate void AddButtonEventHandler(object sender, EventArgs e);

        public delegate void AddRelatedEventHandler(int ProductID, int RelatedID);

        public delegate void EditCommandEventHandler(object source, DataGridCommandEventArgs e);

        public delegate void ItemCommandEventHandler(object source, DataGridCommandEventArgs e);

        public delegate void ReturnButtonEventHandler(object sender, EventArgs e);
    }
}

